<?php if (SET != 1) die(); ?>
	
	<h1>System Neustart</h1>
	
<?php

	$error = false;
	
	
		$ret = exec("sudo /etc/init.d/wlan_firewall 2>&1", $lines);

		if ($ret != "") {
			
			$error = true;
			
			echo '
				<p class="info">
					<strong>Fehlerprotokoll (Firewall Neustart):</strong><br />
					<br />
				';
			
				foreach ($lines as $line) {
					echo $line."<br />";
				}
				
			echo '
				</p>
			';
			
		}
		
		
		
		
		$dhcpConf = file_get_contents("/etc/dhcp3/dhcpd.conf");
		
		if ($dhcpConf) {
			
			$div1 = "###WLAN SECTION - DO NOT DELETE THIS LINE###";
			$div2 = "###END: WLAN SECTION - DO NOT DELETE THIS LINE###";
			
			$pos1 = strpos($dhcpConf, $div1);
			$pos2 = strpos($dhcpConf, $div2);
				
			if ($pos1 !== false AND $pos2 !== false) {
				
				$new = "";
				$i = 2;
				
				foreach (tabFile_load("data/allowed_macs") as $data) {

					$new .= "
						host wlanstatic".$i." {
						  hardware ethernet ".$data[1].";
						  fixed-address 10.0.40.".$i.";
						}

					";

					$i++;

				}
				
				
				$new = "\n\n".$new."\n\n";
				
				$newConf = substr($dhcpConf, 0, $pos1+strlen($div1)).$new.substr($dhcpConf, $pos2);
				
				
				$save = file_put_contents("/etc/dhcp3/dhcpd.conf", $newConf);
				
				if ($save) {
					
					$ret = exec("sudo /etc/init.d/dhcp3-server restart 2>&1", $lines);

					if ($lines[0] != "Stopping DHCP server: dhcpd3." AND $lines[1] != "Starting DHCP server: dhcpd3.") {

						$error = true;

						echo '
							<p class="info">
								<strong>Fehlerprotokoll (DHCP Neustart):</strong><br />
								<br />
							';

							foreach ($lines as $line) {
								echo $line."<br />";
							}

						echo '
							</p>
						';

					}
					
				} else {
					error("Fehler beim Speichern der DHCP-Konfiguration");
					$error = true;
				}
				
			} else {
				error("WLAN-Sektion in der DHCP-Konfiguration fehlt");
				$error = true;
			}
			
		} else {
			error("Fehler beim Öffnen der DHCP-Konfiguration");
			$error = true;
		}
		
		
		
		
			
		if (!$error) {
			
			file_put_contents(config("root")."data/reload", "0");
			echo '<p class="success">Der Neustart wurde ausgeführt. Alle vorgenommenen Änderungen sollten gleich zur Verfügung stehen.</p>';
			
		}
	
?>